package com.yhgm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhgm.common.ErrorCode;
import com.yhgm.constant.CommonConstant;
import com.yhgm.exception.BusinessException;
import com.yhgm.model.dto.role.RoleQueryRequest;
import com.yhgm.model.entity.Roles;
import com.yhgm.service.RolesService;
import com.yhgm.mapper.RolesMapper;
import com.yhgm.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author Gaoming
* @description 针对表【roles(角色)】的数据库操作Service实现
* @createDate 2024-03-22 22:34:37
*/
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles>
    implements RolesService{

    @Override
    public QueryWrapper<Roles> getQueryWrapper(RoleQueryRequest roleQueryRequest) {
        if (roleQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = roleQueryRequest.getId();
        String userRole = roleQueryRequest.getUserRole();
        String name = roleQueryRequest.getName();
        Integer status = roleQueryRequest.getStatus();
        String sortField = roleQueryRequest.getSortField();
        String sortOrder = roleQueryRequest.getSortOrder();

        QueryWrapper<Roles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(userRole), "userRole", userRole);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}




